get the application's resources languages

后端 未结 11 1133
庸人自扰
庸人自扰 2020-12-02 23:30

Is it possible, at runtime, to know which resources languages are embedded in my app?

i.e the presence of this folders:

values-en
values-de
values-fr         


        
11条回答
  •  余生分开走
    2020-12-03 00:19

    Inspired by Mendhak's solution I created something a bit cleaner:

        defaultConfig {
            ....
    
            def locales = ["en", "it", "pl", "fr", "es", "de", "ru"]
    
            buildConfigField "String[]", "TRANSLATION_ARRAY", "new String[]{\""+locales.join("\",\"")+"\"}"
            resConfigs locales
        }
    

    Then in Java use:

    BuildConfig.TRANSLATION_ARRAY
    

    Advatages of this method:

    • Smaller apk - resConfigs will cut out resources from libraries which you don't need (some have hundreds)
    • Fast - No need to parse resource configurations

提交回复
热议问题